import api from './api';
import Vue from 'vue';
import Api from '@/api';
const defaultAlding = {};
Object.keys(Api).forEach(k => {
  defaultAlding[k] = false;
});

// 全局混入
Vue.mixin({
  data() {
    return {
      alding: defaultAlding, //api的loading状态
    };
  },
  computed: {
    g_query() {
      return this.$route.query || {};
    },
  },
  created() {},
  methods: {
    $api(name, params, opts) {
      if (name && Api[name]) {
        this.alding[name] = true;
        return Api[name](params)
          .then(res => {
            this.alding[name] = false;
            return res;
          })
          .catch(e => {
            this.alding[name] = false;
            return e;
          });
      } else {
        return Promise.reject(`${name} not defined in @/api`);
      }
    },
  },
});
